#include <stdio.h>

void sort (int *c, int *w, int n)
{
    int i, tmp, swap;

    swap = 0;
    for (i = n-1; i >= 0; i--) {
        for (tmp = 0; tmp < i; tmp++) {
            if ((float)w[tmp]/c[tmp] < (float)w[tmp+1]/c[tmp+1])
            {
                w[tmp] ^= w[tmp+1];
                w[tmp+1] ^= w[tmp];
                w[tmp] ^= w[tmp+1];

                c[tmp] ^= c[tmp+1];
                c[tmp+1] ^= c[tmp];
                c[tmp] ^= c[tmp+1];

                swap = 1;
            }
        }

        if (swap == 0)
            break;
    }
}

int main(int argc, char const* argv[])
{
    int n = 5;
    int v = 23;
    int c[] = {5, 6, 8, 11, 12};
    int w[] = {19, 24, 33, 45, 50};
    int i, totalw, leftv;
    
    sort(c, w, n);

    for (i = 0; i < n; i++) {
        printf("c[%d]=%-2d\tw[%d]=%d\n", i, c[i], i, w[i]);
    }
    
    totalw = 0; leftv = v;
    for (i = 0; i < n; i++) {
        if (c[i] <= leftv)
        {
            totalw += w[i];
            leftv -= c[i];
        }
    }

    printf("totalw is %d\n", totalw);
    return 0;
}
